package com.lzb.watermarkcamera.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.webkit.WebSettings;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.lzb.watermarkcamera.BaseAc;
import com.lzb.watermarkcamera.R;
import com.lzb.watermarkcamera.databinding.ActivityWebBinding;

public class WebActivity extends BaseAc<ActivityWebBinding> {


    @Override
    protected int onCreate() {
        return R.layout.activity_web;
    }

    @Override
    protected void initView() {
        mDataBinding.ivWebBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        Intent intent = getIntent();
        String title = intent.getStringExtra("title");
        String url = intent.getStringExtra("url");

        mDataBinding.tvTitle.setText(title);

        mDataBinding.webView.removeJavascriptInterface("searchBoxJavaBridge_");
        mDataBinding.webView.removeJavascriptInterface("accessibilityTraversal");
        mDataBinding.webView.removeJavascriptInterface("accessibility");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            mDataBinding.webView.setWebContentsDebuggingEnabled(true);
        }
        mDataBinding.webView.getSettings().setSavePassword(false);
        mDataBinding.webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        // 修改ua使得web端正确判断
        String ua = mDataBinding.webView.getSettings().getUserAgentString();

        //需要隐藏状态栏的页面需要给h5传一个当前设备状态栏高度

        mDataBinding.webView.getSettings().setJavaScriptEnabled(true);
        mDataBinding.webView.getSettings().setAllowFileAccess(true);
        mDataBinding.webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
        mDataBinding.webView.getSettings().setAllowFileAccessFromFileURLs(true);
        mDataBinding.webView.getSettings().setDefaultFontSize(30);
        // webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

        //设置 缓存模式
        mDataBinding. webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        // 开启 DOM storage API 功能
        mDataBinding.webView.getSettings().setDomStorageEnabled(true);
        mDataBinding.webView.getSettings().setSaveFormData(false);
        mDataBinding.webView.getSettings().setLoadsImagesAutomatically(true);
        mDataBinding.webView.getSettings().setBlockNetworkImage(false);//解决图片不显示
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            mDataBinding.webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);


        mDataBinding. webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mDataBinding.webView.getSettings().setSupportZoom(true);
        mDataBinding.webView.getSettings().setBuiltInZoomControls(true);
        mDataBinding.webView.getSettings().setUseWideViewPort(true);
        mDataBinding.webView.getSettings().setSupportMultipleWindows(true);
        mDataBinding.webView.getSettings().setLoadWithOverviewMode(true);
        mDataBinding.webView.getSettings().setGeolocationEnabled(true);
        mDataBinding. webView.getSettings().setDisplayZoomControls(false);
        mDataBinding. webView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);

        mDataBinding.webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        //下面两行代码可以防止webview页打开黑屏

        mDataBinding.webView.loadUrl(url);
    }

    @Override
    protected void initData() {

    }
}